home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / CostManager / CostManager.jar / CostManager.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-09-04  |  15.3 KB  |  449 lines

  1. import java.io.IOException;
  2. import java.util.Calendar;
  3. import java.util.Date;
  4. import java.util.Enumeration;
  5. import java.util.Hashtable;
  6. import javax.microedition.lcdui.Alert;
  7. import javax.microedition.lcdui.AlertType;
  8. import javax.microedition.lcdui.Canvas;
  9. import javax.microedition.lcdui.ChoiceGroup;
  10. import javax.microedition.lcdui.Command;
  11. import javax.microedition.lcdui.CommandListener;
  12. import javax.microedition.lcdui.DateField;
  13. import javax.microedition.lcdui.Display;
  14. import javax.microedition.lcdui.Displayable;
  15. import javax.microedition.lcdui.Form;
  16. import javax.microedition.lcdui.Graphics;
  17. import javax.microedition.lcdui.Image;
  18. import javax.microedition.lcdui.ImageItem;
  19. import javax.microedition.lcdui.Item;
  20. import javax.microedition.lcdui.ItemStateListener;
  21. import javax.microedition.lcdui.List;
  22. import javax.microedition.lcdui.StringItem;
  23. import javax.microedition.lcdui.TextField;
  24. import javax.microedition.midlet.MIDlet;
  25.  
  26. public class CostManager extends MIDlet implements CommandListener, ItemStateListener {
  27.    private Display display = Display.getDisplay(this);
  28.    private Form openingForm = new Form("");
  29.    private Form helpForm;
  30.    private Form resetForm;
  31.    private Form reportsForm;
  32.    private Form textualReportForm;
  33.    private Form graphicReportForm;
  34.    private Form addCostForm;
  35.    private Form addTypeForm;
  36.    private Form delTypeForm;
  37.    private Image img;
  38.    private ImageItem imgItem;
  39.    private ImageItem graphImgItem;
  40.    private List mainList;
  41.    private StringItem resetSI;
  42.    private StringItem openingSI;
  43.    private StringItem helpSI;
  44.    private TextField nameTF;
  45.    private TextField costTF;
  46.    private DateField fromDF;
  47.    private DateField toDF;
  48.    private Calendar calendar = Calendar.getInstance();
  49.    private Canvas graphicReportCanvas;
  50.    private Command helpCommand = new Command("Help", 4, 1);
  51.    private Command nextCommand = new Command("Next", 4, 1);
  52.    private Command backCommand = new Command("Back", 4, 1);
  53.    private Command saveCommand = new Command("Save", 4, 1);
  54.    private Command delCommand = new Command("Del", 4, 1);
  55.    private Command addCommand = new Command("Add", 4, 1);
  56.    private Command okCommand = new Command("Ok", 4, 1);
  57.    private Command mainCommand = new Command("Main", 4, 1);
  58.    private String[] types;
  59.    private CostManager$CostManagerEngine costManagerEngine = new CostManager$CostManagerEngine(this);
  60.    private ChoiceGroup typesCG;
  61.    private ChoiceGroup typesCGCopy;
  62.    private ChoiceGroup reportsCG;
  63.    private Alert alertMessage = new Alert("System Message");
  64.  
  65.    public CostManager() throws Exception {
  66.       try {
  67.          this.img = Image.createImage("/costManager.png");
  68.          this.imgItem = new ImageItem("", this.img, 0, "[CostManager]");
  69.          this.openingForm.append(this.imgItem);
  70.       } catch (IOException var8) {
  71.          ((Throwable)var8).printStackTrace();
  72.       }
  73.  
  74.       this.openingSI = new StringItem("Cost Manager v1.1Beta\nwas developed by", "\nJACADO\nwww.jacado.com\nThis software is provided \"AS IS\", without warranty of any kind. In no event, shell we be liable for any claim, damages or other liability. Copyright(c)2001 Haim Michael, JACADO & ZINDELL Ltd. All rights reserved.");
  75.       this.openingForm.append(this.openingSI);
  76.       this.openingForm.addCommand(this.nextCommand);
  77.       this.openingForm.addCommand(this.helpCommand);
  78.       this.openingForm.setCommandListener(this);
  79.       this.display.setCurrent(this.openingForm);
  80.       System.out.println("openingForm was created");
  81.       this.helpForm = new Form("Help");
  82.       String str = "\nThis midlet allows you to manage your daily costs. You can add new types of costs as well as creating reports (textual & graphic) for specifc period of time. This midlet was not written for a specific currency. Therefore, remember to stick to the currency you chose in the begining. ";
  83.       this.helpSI = new StringItem("Instructions", str);
  84.       this.helpForm.append(this.helpSI);
  85.       this.helpForm.addCommand(this.backCommand);
  86.       this.helpForm.setCommandListener(this);
  87.       System.out.println("helpForm was created");
  88.       this.mainList = new List("Main Menu", 3, new String[]{"Add Cost", "Reports", "Reset", "Add Type", "Del Type", "Exit"}, (Image[])null);
  89.       this.mainList.addCommand(this.nextCommand);
  90.       this.mainList.addCommand(this.backCommand);
  91.       this.mainList.setCommandListener(this);
  92.       System.out.println("mainList was created");
  93.       this.addCostForm = new Form("Add Cost");
  94.       this.typesCG = new ChoiceGroup("", 1, this.types, (Image[])null);
  95.       this.costTF = new TextField("Sum", "", 10, 2);
  96.       this.addCostForm.append(this.typesCG);
  97.       this.addCostForm.append(this.costTF);
  98.       this.addCostForm.addCommand(this.addCommand);
  99.       this.addCostForm.addCommand(this.backCommand);
  100.       this.addCostForm.setCommandListener(this);
  101.       System.out.println("addCostForm was created");
  102.       this.resetForm = new Form("Reset");
  103.       this.resetSI = new StringItem("The result of this action", " All the cost data shell be deleted");
  104.       this.resetForm.append(this.resetSI);
  105.       this.resetForm.addCommand(this.delCommand);
  106.       this.resetForm.addCommand(this.backCommand);
  107.       this.resetForm.setCommandListener(this);
  108.       System.out.println("resetForm was created");
  109.       this.addTypeForm = new Form("Add Type");
  110.       this.nameTF = new TextField("Type name", "", 14, 0);
  111.       this.addTypeForm.append(this.nameTF);
  112.       this.addTypeForm.addCommand(this.addCommand);
  113.       this.addTypeForm.addCommand(this.backCommand);
  114.       this.addTypeForm.setCommandListener(this);
  115.       System.out.println("addTypeForm was created");
  116.       this.reportsForm = new Form("Reports");
  117.       this.fromDF = new DateField("From", 1);
  118.       this.toDF = new DateField("To", 1);
  119.       Date toDate = this.calendar.getTime();
  120.       System.out.println("toDate.getTime()=" + toDate.getTime());
  121.       this.toDF.setDate(toDate);
  122.       Date fromDate = null;
  123.       long subtractFrom = (long)(this.calendar.get(5) - 1) * 24L * 60L * 60L * 1000L;
  124.       System.out.println("subtractFrom=" + subtractFrom);
  125.       long sendToDateConstructor = toDate.getTime() - subtractFrom;
  126.       System.out.println("sendToDateConstructor=" + sendToDateConstructor);
  127.       fromDate = new Date(sendToDateConstructor);
  128.       System.out.println("fromDate.getTime()=" + fromDate.getTime());
  129.       this.fromDF.setDate(fromDate);
  130.       this.reportsCG = new ChoiceGroup("Reports Types", 1, new String[]{"Textual", "Graphics"}, (Image[])null);
  131.       this.reportsForm.append(this.fromDF);
  132.       this.reportsForm.append(this.toDF);
  133.       this.reportsForm.append(this.reportsCG);
  134.       this.reportsForm.addCommand(this.nextCommand);
  135.       this.reportsForm.addCommand(this.backCommand);
  136.       this.reportsForm.setCommandListener(this);
  137.       System.out.println("reportsForm was created");
  138.       this.textualReportForm = new Form("Textual Report");
  139.       this.textualReportForm.addCommand(this.mainCommand);
  140.       this.textualReportForm.addCommand(this.backCommand);
  141.       this.textualReportForm.setCommandListener(this);
  142.       System.out.println("textualReportForm was created");
  143.       this.graphicReportForm = new Form("Graphics Report");
  144.       this.graphicReportForm.addCommand(this.mainCommand);
  145.       this.graphicReportForm.addCommand(this.backCommand);
  146.       this.graphicReportForm.setCommandListener(this);
  147.       System.out.println("graphicReportForm was created");
  148.       this.delTypeForm = new Form("Del Type");
  149.       this.typesCGCopy = new ChoiceGroup("", 1, this.types, (Image[])null);
  150.       this.delTypeForm.append(this.typesCGCopy);
  151.       this.delTypeForm.addCommand(this.delCommand);
  152.       this.delTypeForm.addCommand(this.backCommand);
  153.       this.delTypeForm.setCommandListener(this);
  154.       System.out.println("delTypeForm was created");
  155.    }
  156.  
  157.    public void itemStateChanged(Item item) {
  158.    }
  159.  
  160.    public void commandAction(Command c, Displayable d) {
  161.       if (d == this.openingForm) {
  162.          if (c == this.nextCommand) {
  163.             this.display.setCurrent(this.mainList);
  164.          } else if (c == this.helpCommand) {
  165.             this.display.setCurrent(this.helpForm);
  166.          }
  167.       } else if (d == this.helpForm) {
  168.          if (c == this.backCommand) {
  169.             this.display.setCurrent(this.openingForm);
  170.          }
  171.       } else if (d == this.mainList) {
  172.          if (c == this.nextCommand) {
  173.             if (this.mainList.getString(this.mainList.getSelectedIndex()).equals("Add Cost") && this.typesCG.size() > 0) {
  174.                this.costTF.setString("");
  175.                this.display.setCurrent(this.addCostForm);
  176.             } else if (this.mainList.getString(this.mainList.getSelectedIndex()).equals("Reports")) {
  177.                this.display.setCurrent(this.reportsForm);
  178.             } else if (this.mainList.getString(this.mainList.getSelectedIndex()).equals("Reset")) {
  179.                this.display.setCurrent(this.resetForm);
  180.             } else if (this.mainList.getString(this.mainList.getSelectedIndex()).equals("Del Type")) {
  181.                this.display.setCurrent(this.delTypeForm);
  182.             } else if (this.mainList.getString(this.mainList.getSelectedIndex()).equals("Add Type")) {
  183.                this.display.setCurrent(this.addTypeForm);
  184.             } else if (this.mainList.getString(this.mainList.getSelectedIndex()).equals("Exit")) {
  185.                this.exit();
  186.             }
  187.          } else if (c == this.backCommand) {
  188.             this.display.setCurrent(this.openingForm);
  189.          }
  190.       } else if (d == this.resetForm) {
  191.          if (c == this.backCommand) {
  192.             this.display.setCurrent(this.mainList);
  193.          } else if (c == this.delCommand) {
  194.             try {
  195.                this.costManagerEngine.delAllCosts();
  196.             } catch (Exception var17) {
  197.                ((Throwable)var17).printStackTrace();
  198.             }
  199.  
  200.             this.display.setCurrent(this.mainList);
  201.          }
  202.       } else if (d == this.addCostForm) {
  203.          if (c == this.addCommand) {
  204.             try {
  205.                long time = (new Date()).getTime();
  206.                String typeName = this.typesCG.getString(this.typesCG.getSelectedIndex());
  207.                int sum = Integer.parseInt(this.costTF.getString());
  208.                Cost newCost = new Cost(time, typeName, sum);
  209.                this.costManagerEngine.addCost(newCost);
  210.                this.costTF.setString("");
  211.                this.alertMessage.setString("cost adding succeeded");
  212.                this.alertMessage.setTimeout(2000);
  213.                this.alertMessage.setType(AlertType.INFO);
  214.                this.display.setCurrent(this.alertMessage);
  215.             } catch (Exception var15) {
  216.                this.alertMessage.setString("cost adding failed");
  217.                this.alertMessage.setTimeout(2000);
  218.                this.alertMessage.setType(AlertType.ERROR);
  219.                this.display.setCurrent(this.alertMessage);
  220.                ((Throwable)var15).printStackTrace();
  221.             } finally {
  222.                this.display.setCurrent(this.mainList);
  223.             }
  224.          } else if (c == this.backCommand) {
  225.             this.display.setCurrent(this.mainList);
  226.          }
  227.       } else if (d == this.delTypeForm) {
  228.          if (c == this.delCommand) {
  229.             System.out.println("Del was pressed");
  230.             int temp = this.typesCGCopy.getSelectedIndex();
  231.             System.out.println("The index of the type that was selected is " + temp);
  232.             System.out.println("In typesCG the type to be deleted is " + this.typesCG.getString(temp));
  233.             System.out.println("In typesCGCopy the type to be deleted is " + this.typesCGCopy.getString(temp));
  234.             this.typesCG.delete(temp);
  235.             this.typesCGCopy.delete(temp);
  236.             this.costManagerEngine.removeTypeByName(this.typesCG.getString(temp));
  237.             this.display.setCurrent(this.mainList);
  238.          } else if (c == this.backCommand) {
  239.             this.display.setCurrent(this.mainList);
  240.          }
  241.       } else if (d == this.addTypeForm) {
  242.          if (c == this.backCommand) {
  243.             this.display.setCurrent(this.mainList);
  244.          } else if (c == this.addCommand) {
  245.             try {
  246.                this.costManagerEngine.addType(new Type(this.nameTF.getString()));
  247.             } catch (Exception var14) {
  248.                ((Throwable)var14).printStackTrace();
  249.             }
  250.  
  251.             this.display.setCurrent(this.mainList);
  252.             this.nameTF.setString("");
  253.          }
  254.       } else if (d == this.reportsForm) {
  255.          if (c == this.nextCommand) {
  256.             if (this.reportsCG.getString(this.reportsCG.getSelectedIndex()).equals("Textual")) {
  257.                this.initTextualReportForm();
  258.                this.display.setCurrent(this.textualReportForm);
  259.             } else if (this.reportsCG.getString(this.reportsCG.getSelectedIndex()).equals("Graphics")) {
  260.                this.initGraphicReportForm();
  261.                this.display.setCurrent(this.graphicReportForm);
  262.             }
  263.          } else if (c == this.backCommand) {
  264.             this.display.setCurrent(this.mainList);
  265.          }
  266.       } else if (d == this.textualReportForm) {
  267.          if (c == this.backCommand) {
  268.             this.display.setCurrent(this.reportsForm);
  269.          } else if (c == this.mainCommand) {
  270.             this.display.setCurrent(this.mainList);
  271.          }
  272.       } else if (d == this.graphicReportForm) {
  273.          if (c == this.backCommand) {
  274.             this.display.setCurrent(this.reportsForm);
  275.          } else if (c == this.mainCommand) {
  276.             this.display.setCurrent(this.mainList);
  277.          }
  278.       }
  279.  
  280.    }
  281.  
  282.    public void exit() {
  283.       this.destroyApp(true);
  284.       ((MIDlet)this).notifyDestroyed();
  285.    }
  286.  
  287.    public void startApp() {
  288.       System.out.println("in startApp()");
  289.    }
  290.  
  291.    public void pauseApp() {
  292.       System.out.println("in pause()");
  293.    }
  294.  
  295.    public void destroyApp(boolean cond) {
  296.       this.display = null;
  297.       this.openingForm = null;
  298.       this.helpForm = null;
  299.       this.openingForm = null;
  300.       this.helpForm = null;
  301.       this.resetForm = null;
  302.       this.reportsForm = null;
  303.       this.textualReportForm = null;
  304.       this.graphicReportForm = null;
  305.       this.addCostForm = null;
  306.       this.addTypeForm = null;
  307.       this.delTypeForm = null;
  308.       this.img = null;
  309.       this.imgItem = null;
  310.       this.graphImgItem = null;
  311.       this.mainList = null;
  312.       this.resetSI = null;
  313.       this.openingSI = null;
  314.       this.helpSI = null;
  315.       this.nameTF = null;
  316.       this.costTF = null;
  317.       this.fromDF = null;
  318.       this.toDF = null;
  319.       this.calendar = null;
  320.       this.graphicReportCanvas = null;
  321.       this.helpCommand = null;
  322.       this.nextCommand = null;
  323.       this.backCommand = null;
  324.       this.saveCommand = null;
  325.       this.delCommand = null;
  326.       this.addCommand = null;
  327.       this.okCommand = null;
  328.       this.mainCommand = null;
  329.       this.types = null;
  330.       this.costManagerEngine = null;
  331.       this.typesCG = null;
  332.       this.typesCGCopy = null;
  333.       this.reportsCG = null;
  334.       this.alertMessage = null;
  335.    }
  336.  
  337.    public void initTextualReportForm() {
  338.       Hashtable map = this.costManagerEngine.getCosts();
  339.       int numOfItemsToRemove = this.textualReportForm.size();
  340.  
  341.       for(int i = 0; i < numOfItemsToRemove; ++i) {
  342.          this.textualReportForm.delete(i);
  343.       }
  344.  
  345.       String typeName = null;
  346.       Enumeration e = map.keys();
  347.  
  348.       while(e.hasMoreElements()) {
  349.          typeName = (String)e.nextElement();
  350.          this.textualReportForm.append(new StringItem(typeName + " ", String.valueOf(map.get(typeName))));
  351.       }
  352.  
  353.    }
  354.  
  355.    public void initGraphicReportForm() {
  356.       Hashtable map = this.costManagerEngine.getCosts();
  357.       int numOfItemsToRemove = this.graphicReportForm.size();
  358.  
  359.       for(int i = 0; i < numOfItemsToRemove; ++i) {
  360.          this.graphicReportForm.delete(i);
  361.       }
  362.  
  363.       int numOfTypes = map.size();
  364.       if (numOfTypes < 8) {
  365.          Image temp = Image.createImage(60, 57 + numOfTypes * 15);
  366.          Graphics g = temp.getGraphics();
  367.          g.setColor(255, 255, 255);
  368.          g.fillRect(0, 0, 60, 62 + numOfTypes * 15);
  369.          int[] typeSums = new int[numOfTypes];
  370.          String[] typeNames = new String[numOfTypes];
  371.          int i = 0;
  372.          Enumeration e = map.keys();
  373.          System.out.println("About to start the enumeration process");
  374.  
  375.          while(e.hasMoreElements()) {
  376.             typeNames[i] = (String)e.nextElement();
  377.             System.out.println("typeNames[" + i + "] is : " + typeNames[i]);
  378.             typeSums[i] = (Integer)map.get(typeNames[i]);
  379.             System.out.println("typeSums[" + i + "] is : " + typeSums[i]);
  380.             ++i;
  381.          }
  382.  
  383.          int totalSum = 0;
  384.  
  385.          for(int var15 = 0; var15 < typeSums.length; ++var15) {
  386.             totalSum += typeSums[var15];
  387.          }
  388.  
  389.          System.out.println("The total sum is : " + totalSum);
  390.          int startArc = 0;
  391.          int endArc = 0;
  392.          int[] colors = new int[]{223, 191, 159, 127, 95, 63, 31};
  393.          System.out.println("numOfTypes-1=" + (numOfTypes - 1));
  394.  
  395.          for(i = 0; i < numOfTypes - 1; ++i) {
  396.             System.out.println("startArc=" + startArc + " endArc=" + endArc);
  397.             endArc += 360 * typeSums[i] / totalSum;
  398.             System.out.println("g.setGrayScale(colors[" + i + "])");
  399.             g.setGrayScale(colors[i]);
  400.             System.out.println("g.fillArc(2,2,50,50," + startArc + "," + (endArc - startArc) + ")");
  401.             g.fillArc(2, 2, 50, 50, startArc, endArc - startArc);
  402.             System.out.println("g.fillRect(2," + (70 + i * 15) + ",5,5)");
  403.             g.fillRect(2, 62 + i * 15, 8, 8);
  404.             g.setGrayScale(0);
  405.             System.out.println("g.drawString(typeNames[" + i + "], 2, " + (75 + i * 15) + ", Graphics.LEFT | Graphics.BASELINE)");
  406.             g.drawString(typeNames[i], 12, 60 + i * 15 + 10, 68);
  407.             startArc = endArc;
  408.          }
  409.  
  410.          System.out.println("end of loop. i=" + i);
  411.          endArc = 360;
  412.          g.setGrayScale(colors[i]);
  413.          g.fillArc(2, 2, 50, 50, startArc, endArc - startArc);
  414.          g.fillRect(2, 62 + i * 15, 8, 8);
  415.          g.setGrayScale(0);
  416.          g.drawString(typeNames[i], 12, 60 + i * 15 + 10, 68);
  417.          System.out.println("about to create the imageitem");
  418.          this.graphImgItem = new ImageItem("", Image.createImage(temp), 1, "[Graph]");
  419.          this.graphicReportForm.append(this.graphImgItem);
  420.       }
  421.  
  422.    }
  423.  
  424.    // $FF: synthetic method
  425.    static DateField access$000(CostManager x0) {
  426.       return x0.fromDF;
  427.    }
  428.  
  429.    // $FF: synthetic method
  430.    static DateField access$100(CostManager x0) {
  431.       return x0.toDF;
  432.    }
  433.  
  434.    // $FF: synthetic method
  435.    static String[] access$202(CostManager x0, String[] x1) {
  436.       return x0.types = x1;
  437.    }
  438.  
  439.    // $FF: synthetic method
  440.    static ChoiceGroup access$300(CostManager x0) {
  441.       return x0.typesCG;
  442.    }
  443.  
  444.    // $FF: synthetic method
  445.    static ChoiceGroup access$400(CostManager x0) {
  446.       return x0.typesCGCopy;
  447.    }
  448. }
  449.